#include "Array.h"
#include <cstdlib>
#include <iostream>

Array::Array()
{
    data = NULL;
    length = 0;
};

Array::Array(int _len)
{
    if (_len < 0)
    {
	std::cerr << "The length of an array has to be a non-negtive integer."
		  << std::endl;
	std::exit(-1);
    }
    data = new double[_len];
    length = _len;
};

Array::Array(int _len, double _val)
{
    if (_len < 0)
    {
	std::cerr << "The length of an array has to be a non-negtive integer."
		  << std::endl;
	std::exit(-1);
    }
    data = new double[_len];
    length = _len;
    for (int i = 0; i < length; i++)
	data[i] = _val;
};

Array::~Array()
{
    delete []data;
};

double Array::get_ele_val(int _idx)
{
    // Considering an out-of-range test here. 
    return data[_idx];
};

double* Array::get_ele_add(int _idx)
{
    return (data + _idx);
};

void Array::set_ele_val(int _idx, double _val)
{
    data[_idx] = _val; 
};

void Array::list()
{
    for (int i = 0; i < length; i++)
	std::cout << data[i] << ", ";
    std::cout << "\b\b";
};

void Array::reinit(int _len)
{
    if (_len < 0)
    {
	std::cerr << "The length of an array has to be a non-negtive integer."
		  << std::endl;
	std::exit(-1);
    }
    delete []data;
    data = new double[_len];
    length = _len;
};

void Array::reinit(int _len, double val)
{
    if (_len < 0)
    {
	std::cerr << "The length of an array has to be a non-negtive integer."
		  << std::endl;
	std::exit(-1);
    }
    delete []data;
    data = new double[_len];
    length = _len;
};

int Array::get_size()
{
    return length;
};
